import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CmtContentPostApi } from '#/api/cmt/content/post';

import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getRangePickerDefaultProps } from '#/utils';
import { getBaseCommunityOptions } from '#/api/cmt/base/community';
import { getMerchantInfoOptions } from '#/api/cmt/base/merchantInfo';
import { getPostCategoryOptions } from '#/api/cmt/content/postCategory';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'communityId',
      label: '社区',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择社区',
        api: getBaseCommunityOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择商户',
        api: getMerchantInfoOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'userId',
      label: '发布会员',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入发布会员ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'categoryId',
      label: '分类',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择帖子分类',
        api: getPostCategoryOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'postType',
      label: '帖子类型',
      component: 'RadioGroup',
      formItemClass: 'col-span-2',
      componentProps: {
        options: [
          { label: '动态', value: 1 },
          { label: '建议', value: 2 },
          { label: '资讯', value: 3 },
          { label: '活动回顾', value: 4 },
        ],
      },
      defaultValue: 1,
    },
    {
      fieldName: 'title',
      label: '标题',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入帖子标题',
      },
    },
    {
      fieldName: 'content',
      label: '内容',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 6,
        placeholder: '请输入帖子内容',
      },
    },
    {
      fieldName: 'images',
      label: '图片',
      component: 'ImageUpload',
      formItemClass: 'col-span-2',
      componentProps: {
        multiple: true,
      },
    },
    {
      fieldName: 'tags',
      label: '标签',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入标签，多个标签用逗号分隔',
      },
    },
    {
      fieldName: 'isTop',
      label: '是否置顶',
      component: 'RadioGroup',
      formItemClass: 'col-span-1',
      componentProps: {
        options: [
          { label: '否', value: 0 },
          { label: '是', value: 1 },
        ],
      },
      defaultValue: 0,
    },
    {
      fieldName: 'isHot',
      label: '是否热门',
      component: 'RadioGroup',
      formItemClass: 'col-span-1',
      componentProps: {
        options: [
          { label: '否', value: 0 },
          { label: '是', value: 1 },
        ],
      },
      defaultValue: 0,
    },
    {
      fieldName: 'isEssence',
      label: '是否精华',
      component: 'RadioGroup',
      formItemClass: 'col-span-2',
      componentProps: {
        options: [
          { label: '否', value: 0 },
          { label: '是', value: 1 },
        ],
      },
      defaultValue: 0,
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'title',
      label: '标题',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入标题',
      },
    },
    {
      fieldName: 'userId',
      label: '发布会员',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入发布会员ID',
      },
    },
    {
      fieldName: 'categoryId',
      label: '分类',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请选择分类',
      },
    },
    {
      fieldName: 'postType',
      label: '帖子类型',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: [
          { label: '动态', value: 1 },
          { label: '建议', value: 2 },
          { label: '资讯', value: 3 },
          { label: '活动回顾', value: 4 },
        ],
        placeholder: '请选择帖子类型',
      },
    },
    {
      fieldName: 'auditStatus',
      label: '审核状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: [
          { label: '待审核', value: 0 },
          { label: '通过', value: 1 },
          { label: '拒绝', value: 2 },
        ],
        placeholder: '请选择审核状态',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<CmtContentPostApi.Post>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '帖子ID',
      minWidth: 100,
    },
    {
      field: 'title',
      title: '标题',
      minWidth: 200,
    },
    {
      field: 'userId',
      title: '发布会员',
      minWidth: 100,
    },
    {
      field: 'postType',
      title: '帖子类型',
      minWidth: 100,
      slots: { default: 'postType' },
    },
    {
      field: 'categoryId',
      title: '分类ID',
      minWidth: 100,
    },
    {
      field: 'content',
      title: '内容',
      minWidth: 200,
    },
    {
      field: 'viewCount',
      title: '浏览数',
      minWidth: 100,
    },
    {
      field: 'likeCount',
      title: '点赞数',
      minWidth: 100,
    },
    {
      field: 'commentCount',
      title: '评论数',
      minWidth: 100,
    },
    {
      field: 'isTop',
      title: '置顶',
      minWidth: 80,
      slots: { default: 'isTop' },
    },
    {
      field: 'isHot',
      title: '热门',
      minWidth: 80,
      slots: { default: 'isHot' },
    },
    {
      field: 'isEssence',
      title: '精华',
      minWidth: 80,
      slots: { default: 'isEssence' },
    },
    {
      field: 'auditStatus',
      title: '审核状态',
      minWidth: 100,
      slots: { default: 'auditStatus' },
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
